package com.vincent.kdp.ui.fragment.presenter;

import android.content.Intent;
import android.text.TextUtils;

import com.vincent.baseapp.base.BasePresenter;
import com.vincent.baseapp.util.SPUtil;
import com.vincent.kdp.bean.MyBanner;
import com.vincent.kdp.bean.MySeriesAdapterBean;
import com.vincent.kdp.bean.QualityBean;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.ui.bean.entity.Banner;
import com.vincent.kdp.ui.bean.entity.NewCommodity;
import com.vincent.kdp.ui.bean.response.HotCommodityListOutput;
import com.vincent.kdp.ui.bean.response.NewCommodityListOutput;
import com.vincent.kdp.ui.fragment.contract.CommodityContract;
import com.vincent.kdp.ui.fragment.repository.CommodityRepository;
import com.vincent.kdp.ui.fragment.repository.NewRepository;
import com.vincent.kdp.util.RegexUtils;

import java.util.ArrayList;
import java.util.List;

public class CommodityPresenter extends BasePresenter<CommodityContract.View> implements CommodityContract.Presenter {
    private List<MyBanner> mBannerList;
    private CommodityRepository mRepository;
    private NewRepository newRepository;

    public CommodityPresenter() {
        mRepository = new CommodityRepository();
        newRepository = new NewRepository();
        mBannerList = new ArrayList<>();

        String listString= RegexUtils.trimNull(SPUtil.getString("newFirstImageList"));
        String listIdString= RegexUtils.trimNull(SPUtil.getString("newIdList"));
        String listNameString= RegexUtils.trimNull(SPUtil.getString("newNameList"));
        if(!listString.isEmpty()){
            String[] images=listString.split(",");
            String[] id=listIdString.split(",");
            String[] name=listNameString.split(",");
            /*for(String img:images){
                Banner banner=new Banner();
                banner.setItemImgUrl(img);
                mBannerList.add(banner);
            }*/
            for(int x=0;x<images.length;x++){
                MyBanner banner=new MyBanner();
                banner.setItemImgUrl(images[x]);
                banner.setItemScroId(id[x]);
                banner.setItemName(name[x]);
                mBannerList.add(banner);
            }
        }
    }

    @Override
    public void initView(Intent intent) {
        //mView.initBannerViewPager(mBannerList);
        mView.initScrollViewPager(mBannerList);
        reqHotCommodityList();

    }

    /**
     * 获取热门商品列表
     */
    private void reqHotCommodityList() {
        mView.showLoadDialog();
        mRepository.reqHotCommodityList(new RequestCallBack() {
            @Override
            public void onSuccess(Object object) {
                HotCommodityListOutput hotCommodityListOutput = (HotCommodityListOutput) object;
                if (mView != null) {
                    mView.dismissLoadDialog();
                    if (hotCommodityListOutput.getData() != null) {
                        mView.initHotCommodities(hotCommodityListOutput.getData());
                    }
                }
            }

            @Override
            public void onFalse(String msg) {
                if (mView != null) {
                    mView.dismissLoadDialog();
                    mView.showToast(msg);
                }
            }
        });
    }
}
